package com.websocket.socket;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import com.websocket.util.ChatRoomUtil;

@ServerEndpoint("/sendMessage2Allw")
public class SendMessage2All {
	
	@OnOpen
	public void onOpen(Session session){
		ChatRoomUtil.addOneSession(session); 
		ChatRoomUtil.getAllUserJsonInfo();
		System.out.println("有新连接加入！当前在线人数为" + ChatRoomUtil.getUserNum());
    }
	
	@OnClose
	public void onClose(Session session){
		ChatRoomUtil.removeOneSession(session);
		ChatRoomUtil.getAllUserJsonInfo();
		System.out.println("某用户退出！当前在线人数为" +   ChatRoomUtil.getUserNum());
    }
	
	@OnMessage
	public void onMessage(String message, Session session) {
		System.out.println("来自客户端的消息:" + message);
		ChatRoomUtil.sendMassage2All(message);
     }
}